package me.zhengjie.pm.projectstageprogress.service.impl;

import me.zhengjie.pm.projectstageprogress.domain.ProjectStageProgress;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectstageprogress.repository.ProjectStageProgressRepository;
import me.zhengjie.pm.projectstageprogress.service.ProjectStageProgressService;
import me.zhengjie.pm.projectstageprogress.service.dto.ProjectStageProgressDto;
import me.zhengjie.pm.projectstageprogress.service.dto.ProjectStageProgressQueryCriteria;
import me.zhengjie.pm.projectstageprogress.service.mapper.ProjectStageProgressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "projectStageProgress")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectStageProgressServiceImpl implements ProjectStageProgressService {

    private final ProjectStageProgressRepository projectStageProgressRepository;

    private final ProjectStageProgressMapper projectStageProgressMapper;

    public ProjectStageProgressServiceImpl(ProjectStageProgressRepository projectStageProgressRepository, ProjectStageProgressMapper projectStageProgressMapper) {
        this.projectStageProgressRepository = projectStageProgressRepository;
        this.projectStageProgressMapper = projectStageProgressMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectStageProgressQueryCriteria criteria, Pageable pageable){
        Page<ProjectStageProgress> page = projectStageProgressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectStageProgressMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectStageProgressDto> queryAll(ProjectStageProgressQueryCriteria criteria){
        return projectStageProgressMapper.toDto(projectStageProgressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectStageProgressDto findById(Integer id) {
        ProjectStageProgress projectStageProgress = projectStageProgressRepository.findById(id).orElseGet(ProjectStageProgress::new);
        ValidationUtil.isNull(projectStageProgress.getId(),"ProjectStageProgress","id",id);
        return projectStageProgressMapper.toDto(projectStageProgress);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectStageProgressDto create(ProjectStageProgress resources) {
        return projectStageProgressMapper.toDto(projectStageProgressRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectStageProgress resources) {
        ProjectStageProgress projectStageProgress = projectStageProgressRepository.findById(resources.getId()).orElseGet(ProjectStageProgress::new);
        ValidationUtil.isNull( projectStageProgress.getId(),"ProjectStageProgress","id",resources.getId());
        projectStageProgress.copy(resources);
        projectStageProgressRepository.save(projectStageProgress);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectStageProgressRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectStageProgressDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectStageProgressDto projectStageProgress : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目ID", projectStageProgress.getProjectId());
            map.put("项目阶段ID", projectStageProgress.getStageId());
            map.put("任务累计完成进度", projectStageProgress.getProgress());
            map.put("填报时间", projectStageProgress.getUpdateTime());
            map.put("开始时间", projectStageProgress.getStartTime());
            map.put("结束时间", projectStageProgress.getEndTime());
            map.put("此周期项目所耗时（人/天）", projectStageProgress.getTimeUse());
            map.put("具体描述", projectStageProgress.getDescribe());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}